home *** CD-ROM | disk | FTP | other *** search
/ SGI Hot Mix 18 / Silicon_Graphics_hot mix 18.iso / html / vendors / NETRICS.COM / Utilities / rip < prev   
Encoding:
Text File  |  1997-12-31  |  2.9 KB  |  154 lines

  1. #!/usr/bin/perl
  2. #change the line above to the location of the perl interpreter on your system
  3.  
  4. # rip
  5. #
  6. # Author   Michel G Delory
  7. # Created  April 1997
  8. # Last Rev May 1 1997
  9. #
  10. # resolve numerical IP addresses in a given log file
  11. # usage:
  12. # rip [-f] input_log_file [output_log_file]
  13. # if [output log file] is omitted, the changes will be made 
  14. # in place, in the original file
  15. # the -f option is used to force resolution of previously unresolved addresses
  16. # otherwise  rip will only attempt to resolve numerical IP's once
  17. # resolved IP's are stored in a flat file called rip.db
  18. #
  19.  
  20. $usage = "rip\nusage: rip [-f] input_log_file [output_log_file]";
  21. $usage .= "\n\t-f force resolver on previously unresolved addresses\n";
  22. $usage .= "\toutput_log_file can be omitted for in place resolving\n";
  23. $inPlace = 0;
  24.  
  25. $arg1 = shift @ARGV || die $usage;
  26. if ($arg1 eq "-f")
  27. {
  28.     $forceRes = 1;
  29.     $inFile = shift @ARGV || die $usage;
  30. }
  31. else
  32. {
  33.     $inFile = $arg1;
  34.     $forceRes = 0;
  35. }
  36. if (!defined ($outFile = shift @ARGV))
  37. {
  38.     $outFile = "temp.log";
  39.     $inPlace = 1;
  40. }
  41.  
  42. if (!open (IN, "$inFile"))
  43. {
  44.     die "rip\nCan't open input log file $inFile\n$!\n";
  45. }
  46.  
  47. if (!open (OUT, ">$outFile"))
  48. {
  49.     close (IN);
  50.     die "rip\nCan't open/create output log file $outFile\n$!\n";
  51. }
  52. chmod 0666, $outFile;
  53.  
  54. if (!open (DB, "rip.db"))
  55. {
  56.     %resolvedHost = ();
  57. }
  58. else
  59. {
  60.     while ($line = <DB>)
  61.     {
  62.     ($ip, $host) = split (/\s+/, $line);
  63.     if ($ip ne $host || $forceRes == 0)
  64.     {
  65.         # only include addresses that are really resolved, 
  66.         # unless the force resolve flag is not set
  67.         $resolvedHost{$ip} = $host;
  68.     }
  69.     }
  70.     close (DB);
  71. }
  72.  
  73. print "rip: analysing $inFile\n";
  74. logline:
  75. while ($line = <IN>)
  76. {
  77.     if ($line =~ /^(\S*)/)        
  78.     {
  79.  
  80.     if (int($1) != 0)
  81.     {
  82.         if ($resolved = ($resolvedHost{$1}))
  83.         {
  84.         # this address already in resolved list
  85.         $line = $resolved.$';
  86.         }
  87.         else 
  88.         {
  89.         # try to resolve it once
  90.         if ($host = do ResolveIP ($1))
  91.         {    
  92.             $resolvedHost{$1} = $host;
  93.             $line = $host.$';
  94.             print "rip: $1 -> $host\n";
  95.         }    
  96.         else
  97.         {
  98.             # insert unresolved IP in resolved list
  99.             $resolvedHost{$1} = $1;
  100.         }
  101.         
  102.         }
  103.         
  104.     }
  105.     
  106.     print OUT $line;
  107.     
  108.     }
  109.     
  110. }
  111.  
  112.  
  113. close (IN);
  114. close (OUT);
  115.  
  116. if (!open (DB, ">rip.db"))
  117. {
  118.     die "rip\nCan't save resolved hosts in rip.db\n$!\n";
  119. }
  120. else
  121. {
  122.     chmod 0666, "rip.db";
  123.     
  124.     foreach $key (keys (%resolvedHost))
  125.     {
  126.     print DB $key, " ", $resolvedHost{$key}, "\n";
  127.     }
  128.     close (DB);
  129. }
  130.  
  131. if ($inPlace == 1)
  132. {
  133.     if (rename ($outFile, $inFile) == 0)
  134.     {
  135.     die "rip\nCan't overwrite $inFile\n$!\nResolved Log is now stored in the file $outFile\n";
  136.     }
  137. }
  138.  
  139.  
  140. sub ResolveIP
  141. {
  142.     local ($ip) = @_;
  143.     local ($addNum);
  144.  
  145.     $AF_INET = 2;
  146.  
  147.     $addNum = pack ('C4', split (/\./, $ip));
  148.     (gethostbyaddr ($addNum, $AF_INET))[0];
  149. }
  150.  
  151.  
  152.  
  153.  
  154.